<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        echo 'index';exit;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->input('data');
        if(empty($data)){
            return false;
        }

        $user = new User();
        $user->name = $data['name'];
        $user->open_id =  $data['openid'];
        $user->email = $data['email'];
        $user->password = 'password';
        $user->company = $data['company'];
        $user->title = $data['title'];
        $user->number_of_stores = $data['number_of_stores'];

        $user->save();
        echo json_encode($data);exit;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $openId = $request->input('openId');
        if(empty($openId)){
            return '0';
        }
        $result =  User::where('open_id', $openId)->first();
        $return  =array(
            'name' => $result->name,
            'company' => $result->company,
            'title' => $result->title,
            'number_of_stores' => $result->number_of_stores,
            'email' => $result->email,
        );

        return json_encode($return);exit;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $data = $request->input('data');
        if(!$data){
            return 0;
        }

        $result =  User::where('open_id', $data['openId'])->first();
        $result->name = $data['name'] ? $data['name'] : $result->name;
        $result->open_id =  $data['openId'];
        $result->email = $data['email'] ? $data['email'] : $result->email;
        $result->password = 'password';
        $result->company = $data['company'] ? $data['company'] : $result->company;
        $result->title = $data['title'] ? $data['title'] : $result->title;
        $result->number_of_stores = $data['number_of_stores'] ? $data['number_of_stores'] : $result->number_of_stores;
        $result->save();

        return 'true';exit;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
